package servlet;

import dao.CardDao;
import dao.ManagerDao;
import dao.StudentDao;
import pojo.Card;
import pojo.Manager;
import pojo.Student;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

/**
 * @author konley
 * @date 2020-06-09 20:15
 */
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String url = null;
        String msg = null;
        //对验证码进行转小写处理，达到不区分大小写的效果
        String realcode = request.getSession().getAttribute("realcode").toString().toLowerCase();
        //保存用户输入的信息
        String inputCode = request.getParameter("checkcode").toLowerCase();
        String inputId = request.getParameter("id");
        String inputPsw = request.getParameter("password");
        if (realcode.equals(inputCode)) { //验证码输入正确.
            //获取用户输入的id和登录类别
            int id = Integer.parseInt(request.getParameter("id"));
            String m = request.getParameter("m");
            if (m.equals("student")) {    //学生登录
                //准备学生和饭卡信息
                Student student = new StudentDao().getById(id);
                Card card = new CardDao().getCard(id);
                if (student == null) {
                    //用户不存在，清空用户输入的id
                    inputId = "";
                    url = "/index.jsp";
                    msg = "用户不存在";
                } else if (!student.getPassword().equals(inputPsw)) {
                    //密码错误，清空用户输入的密码
                    inputPsw = "";
                    url = "/index.jsp";
                    msg = "密码错误";
                }else{  //成功登录
                    //存入学生信息和饭卡信息
                    request.getSession(true).setAttribute("student",student);
                    request.getSession(true).setAttribute("card",card);
                    url="/studentView.jsp";
                }
            }else{  //管理员登录
                //创建管理员类
                Manager manager = new ManagerDao().getManager(id);
                if (manager == null) {
                    inputId = "";
                    url = "/index.jsp";
                    msg = "用户不存在";
                } else if (!manager.getPassword().equals(inputPsw)) {
                    url = "/index.jsp";
                    msg = "密码错误";
                }else{  //成功登录
                    //查统计信息并存入session，管理员登录即可看到
                    List<Integer> lists = new ManagerDao().selectDb();
                    request.getSession(true).setAttribute("moneySum", lists.get(0));
                    request.getSession(true).setAttribute("billSum", lists.get(1));
                    request.getSession(true).setAttribute("stuSum", lists.get(2));
                    request.getSession(true).setAttribute("cardSum", lists.get(3));
                    request.getSession(true).setAttribute("managerSum", lists.get(4));
                    request.getSession(true).setAttribute("money1Sum", lists.get(5));
                    request.getSession(true).setAttribute("money2Sum", lists.get(6));
                    request.getSession(true).setAttribute("money3Sum", lists.get(7));
                    //存入管理员信息
                    request.getSession(true).setAttribute("manager",manager);
                    url="/managerView.jsp";
                }
            }
        } else {  //验证码输入错误
            //验证码会刷新，所以没有保留用户输入的必要
            url = "/index.jsp";
            msg = "验证码输入有误";
        }
        //存入当前用户输入的账号（错了会被清空，正确的留下来）
        request.setAttribute("inputId",inputId);
        request.setAttribute("inputPsw",inputPsw);
        request.setAttribute("msg", msg);
        request.getRequestDispatcher(url).forward(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

}
